package com.wst.system.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wst.system.config.exception.GlobalExcetion;
import com.wst.system.security.IsAdmin;
import com.wst.system.security.entity.SysRoleEntity;
import com.wst.system.security.entity.vo.SysRoleEntityVo;
import com.wst.system.server.entity.vo.DelManyDataVo;
import com.wst.system.server.entity.vo.PageSearchVo;
import com.wst.system.server.service.SysRoleService;
import com.wst.system.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping("/page")
    public JSONObject page(@RequestBody @Validated PageSearchVo vo){
        IPage<SysRoleEntity> page=sysRoleService.page(vo.getPageNum(), vo.getPageSize(), vo);
        return ResultUtil.successInfoTable(page.getTotal(), page.getRecords());
    }

    @IsAdmin
    @PostMapping("/addorupdate")
    public JSONObject addorupdate(@RequestBody @Validated SysRoleEntityVo vo) throws GlobalExcetion {
        sysRoleService.saveOrUpdateSysRole(vo);
        return ResultUtil.successInfo();
    }


    @IsAdmin
    @PostMapping("delete")
    public JSONObject delete(@RequestBody DelManyDataVo vo) throws GlobalExcetion {
        sysRoleService.deleteIds(vo.getIds());
        return ResultUtil.successInfo("删除成功");
    }

    @IsAdmin
    @GetMapping("getrolelist")
    public JSONObject getRoleList(@RequestParam(value = "name",required = false)String name){
        List<SysRoleEntity> roleEntities=sysRoleService.getByRoleName(name);
        return ResultUtil.successInfo(roleEntities);
    }
}
